package cn.leida.ruoyi.system.bean;

import cn.leida.ruoyi.common.config.AppConfig;
import cn.leida.ruoyi.common.core.domain.entity.SysUser;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lei
 * @create 2025-03-06 11:09
 */
public class SysUserSqlBean extends BaseSqlBean {

    private SysUser sysUser = new SysUser();

    public SysUserSqlBean(SysUser sysUser) {
        this.sysUser = sysUser;
        buildSelect();
        buildFrom();
        buildWhere();
    }

    @Override
    public String countSql() {
        return "select count(*) dataCount from (" + select.toString() + where.toString() + ") tab";
    }

    @Override
    public String selectSql() {
        return select.toString() + where.toString() + buildOrder();
    }

    @Override
    public String buildOrder() {
        return "  ";
    }

    private String buildSelect() {
        select.append(" select u.user_id userId, ");
        select.append(" u.dept_id deptId, ");
        select.append(" u.nick_name nickName, ");
        select.append(" u.user_name userName, ");
        select.append(" u.email email, ");
        select.append(" u.avatar avatar, ");
        select.append(" u.phone_number phoneNumber, ");
        select.append(" u.password password, ");
        select.append(" u.sex sex, ");
        select.append(" u.status status, ");
        select.append(" u.del_flag delFlag, ");
        select.append(" u.login_ip loginIp, ");
        select.append(" u.login_date loginDate, ");
        select.append(" u.create_by createBy, ");
        select.append(" u.create_time createTime, ");
        select.append(" u.update_time updateTime, ");
        select.append(" u.update_by updateBy, ");
        select.append(" u.remark remark, ");
        select.append(" d.dept_name deptName, ");
        select.append(" d.leader leader ");
        return select.toString();
    }

    private void buildFrom() {
        select.append(" from sys_user u  ");
        select.append(" left join sys_dept d on u.dept_id = d.dept_id  ");
    }

    private void buildWhere() {
        where.append("  where u.del_flag = '0'  ");
        if (StringUtils.isNotBlank(sysUser.getUserName())) {
            where.append("   AND u.user_name like ? ");
            params.add("%" + sysUser.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(sysUser.getStatus())) {
            where.append("   AND u.status = ? ");
            params.add(sysUser.getStatus());
        }
        if (StringUtils.isNotBlank(sysUser.getPhoneNumber())) {
            where.append("   AND u.phone_number like ? ");
            params.add("%" + sysUser.getPhoneNumber() + "%");
        }
        if (null != sysUser.getDeptId()) {
            if (AppConfig.sqlite()) {
                where.append("   AND   u.dept_id = ?" );
                params.add(sysUser.getDeptId());
            } else {
                where.append("   AND (u.dept_id = ? OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(?, ancestors) )) ");
                params.add(sysUser.getDeptId());
                params.add(sysUser.getDeptId());
            }
        }

        if (null != sysUser.getParams().get("beginTime")) {
            if (!AppConfig.sqlite()) {
                where.append("   AND date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ");
                params.add(sysUser.getParams().get("beginTime"));
            }

        }
        if (null != sysUser.getParams().get("endTime")) {
            if (!AppConfig.sqlite()) {
                where.append("   AND date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ");
                params.add(sysUser.getParams().get("endTime"));
            }
        }
        where.append(sysUser.getParams().getOrDefault("dataScope", ""));
    }


}
